package com.ruoyi.park.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.park.domain.BuiGreenArea;
import com.ruoyi.park.domain.BuiPark;
import com.ruoyi.park.domain.vo.GreenInfoExcelVO;
import com.ruoyi.park.domain.vo.ParkInfoVO;
import com.ruoyi.park.mapper.BuiGreenAreaMapper;
import com.ruoyi.park.service.IBuiGreenAreaService;
import com.ruoyi.park.service.IBuiParkService;
import com.ruoyi.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.mapper.WyGreenInfoMapper;
import com.ruoyi.park.domain.WyGreenInfo;
import com.ruoyi.park.service.IWyGreenInfoService;

import javax.management.Query;

/**
 * 绿化信息Service业务层处理
 *
 * @author cjie
 * @date 2021-07-22
 */
@Service
public class WyGreenInfoServiceImpl implements IWyGreenInfoService {

    @Autowired
    private WyGreenInfoMapper wyGreenInfoMapper;

    @Autowired
    private IBuiGreenAreaService buiGreenAreaService;

    @Autowired
    private IBuiParkService parkService;

    @Autowired
    private RemoteDictService remoteDictService;

    /**
     * 查询绿化信息
     *
     * @param id 绿化信息ID
     * @return 绿化信息
     */
    @Override
    public WyGreenInfo selectWyGreenInfoById(String id) {
        return wyGreenInfoMapper.selectWyGreenInfoById(id);
    }

    /**
     * 查询绿化信息列表
     *
     * @param wyGreenInfo 绿化信息
     * @return 绿化信息
     */
    @Override
    public List<WyGreenInfo> selectWyGreenInfoList(WyGreenInfo wyGreenInfo) {
        return wyGreenInfoMapper.selectWyGreenInfoList(wyGreenInfo);
    }

    /**
     * 新增绿化信息
     *
     * @param wyGreenInfo 绿化信息
     * @return 结果
     */
    @Override
    public int insertWyGreenInfo(WyGreenInfo wyGreenInfo) {
        wyGreenInfo.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyGreenInfo.setCreateTime(DateUtils.getNowDate());
        wyGreenInfo.setCreateBy(SecurityUtils.getUsername());
        return wyGreenInfoMapper.insertWyGreenInfo(wyGreenInfo);
    }

    /**
     * 修改绿化信息
     *
     * @param wyGreenInfo 绿化信息
     * @return 结果
     */
    @Override
    public int updateWyGreenInfo(WyGreenInfo wyGreenInfo) {
        wyGreenInfo.setUpdateTime(DateUtils.getNowDate());
        wyGreenInfo.setUpdateBy(SecurityUtils.getUsername());
        return wyGreenInfoMapper.updateWyGreenInfo(wyGreenInfo);
    }

    /**
     * 批量删除绿化信息
     *
     * @param ids 需要删除的绿化信息ID
     * @return 结果
     */
    @Override
    public int deleteWyGreenInfoByIds(String[] ids) {
        return wyGreenInfoMapper.deleteWyGreenInfoByIds(ids);
    }

    /**
     * 删除绿化信息信息
     *
     * @param id 绿化信息ID
     * @return 结果
     */
    @Override
    public int deleteWyGreenInfoById(String id) {
        return wyGreenInfoMapper.deleteWyGreenInfoById(id);
    }

    @Override
    public String importData(List<GreenInfoExcelVO> list) {
        list = list.stream().filter(o -> StringUtils.isNotBlank(o.getPlantName())).collect(Collectors.toList());
        Map<String, List<GreenInfoExcelVO>> map = list.stream().collect(Collectors.groupingBy(GreenInfoExcelVO::getAreaNum));
        for (Map.Entry<String, List<GreenInfoExcelVO>> entry : map.entrySet()) {
            String code = entry.getKey();
            List<GreenInfoExcelVO> greenInfoExcelVOS = entry.getValue();
            BuiGreenArea query = new BuiGreenArea();
            query.setAreaNum(code);
            String id = null;
            String parkId = null;
            List<BuiGreenArea> rs = buiGreenAreaService.selectBuiGreenAreaList(query);
            if (rs.size() == 0) {
                if (greenInfoExcelVOS.size() > 0) {
                    query.setAreaScope(greenInfoExcelVOS.get(0).getAreaScope());
                    query.setAreaName(greenInfoExcelVOS.get(0).getAreaName());
                    query.setAreaSize(greenInfoExcelVOS.get(0).getArea());
                    List<BuiPark> parks = parkService.selectBuiParkByCode(greenInfoExcelVOS.get(0).getParkNum());
                    if (parks.size() > 0) {
                        query.setParkId(parks.get(0).getId());
                        parkId = parks.get(0).getId();
                    }
                    id = IdUtil.getSnowflake(0, 0).nextIdStr();
                    query.setId(id);
                    buiGreenAreaService.insertBuiGreenArea(query);
                }
            } else {
                id = rs.get(0).getId();
            }
            for (GreenInfoExcelVO o : greenInfoExcelVOS) {
                //查询VO是否存在
                WyGreenInfo greenQuery = new WyGreenInfo();
                greenQuery.setPlantName(o.getPlantName());
                greenQuery.setGreenAreaId(id);
                List<WyGreenInfo> qList = selectWyGreenInfoList(greenQuery);
                qList.forEach(a -> {
                    deleteWyGreenInfoById(a.getId());
                });
                WyGreenInfo wyGreenInfo = new WyGreenInfo();
                wyGreenInfo.setPlantName(o.getPlantName());
                wyGreenInfo.setAmount(o.getAmount());
                wyGreenInfo.setGreenAreaId(id);
                wyGreenInfo.setParkId(parkId);
                insertWyGreenInfo(wyGreenInfo);

            }
        }

        //同步所有字典数据
        List<GreenInfoExcelVO> infoExcelVOS = list.stream().collect(Collectors.toMap(GreenInfoExcelVO::getPlantName, o -> o, (o1, o2) -> {
            return o1;
        })).values().stream().collect(Collectors.toList());
        infoExcelVOS.forEach(o -> {
            remoteDictService.insertDictData("plants", o.getPlantName());
        });
        return "导入成功";
    }
}
